<template>
  <div class="bg">
    <div class="login_box">
      <div class="left">
        <div class="img_box">
          <img src="../assets/img/undraw_login_re_4vu2 1.png" alt="">
        </div>
      </div>
      <div class="right">
        <p>欢迎注册创思设备管理系统</p>
        <el-form :model="registerForm"
         :rules="registerRules"
          ref="registerFormRef"
          label-position="top"
          class="regis_form"
          >
          <el-form-item label="姓名" prop="name" class="item">
            <el-input type="text" v-model.trim="registerForm.name"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email" class="item">
            <el-input v-model.trim="registerForm.email"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" class="item">
            <el-input :type="classes" v-model.trim="registerForm.password"  @change="check">
              <i slot="suffix" class="el-icon-view" @click="showPasswd"></i>
            </el-input>
          </el-form-item>
          <el-form-item>
            <div class="tip">
              <span class="level">密码强度:</span>
              <span :class="pwdLevel">{{LevelCon}}</span>
            </div>
          </el-form-item>
          <el-checkbox v-model="registerForm.checked" class="checkbox">我已阅读用户协议并同意</el-checkbox>
          <el-form-item class="btns">
            <el-button type="primary" @click="register">注册</el-button>
          </el-form-item>
        </el-form>
        <div class="bottom">
          <span>您已有平台账号?</span>
          <el-button type="text" @click="toLogin"><span>用户登录</span></el-button>
        </div>
        <div class="forget">
          <el-button type="text" @click="forget">忘记密码</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    // 邮箱验证规则
    var checkEmail = (rules, value, cb) => {
      const regEmail = /^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/
      if (regEmail.test(value)) {
        return cb()
      } else {
        return cb(new Error('请输入合法的邮箱'))
      }
    }
    return {
      registerForm: {
        email: '',
        password: '',
        name: ''
      },
      classes: 'password',
      checked: false,
      registerRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 8, max: 15, message: '长度在 8 到 20 个字符', trigger: 'blur' }
        ]
      },
      pwdLevel: '',
      LevelCon: ''
    }
  },
  methods: {
    // 密码明文密文转换
    showPasswd () {
      // 点击图标是密码隐藏或显示
      if (this.classes === 'text') {
        this.classes = 'password'
        // 更换图标
      } else {
        this.classes = 'text'
      }
    },
    // 判断密码强度
    check () {
      // 弱密码：全是数字或全是字母，8-20个字符
      const weakReg = /^[0-9]{8,20}$|^[a-zA-Z]{8,20}$/

      // 中密码：至少一个字母和一个数字
      const mediumReg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/

      // 强密码：至少一个字母，一个数字和一个特殊字符
      const strongReg = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,20}$/
      // console.log('this.registerForm.password.match(strongReg) ', this.registerForm.password.match(strongReg))
      if (this.registerForm.password.length >= 8 && this.registerForm.password.length <= 20) {
        if (this.registerForm.password.match(weakReg)) {
          this.pwdLevel = 'one'
          this.LevelCon = '简单'
        } else if (this.registerForm.password.match(mediumReg)) {
          this.pwdLevel = 'two'
          this.LevelCon = '复杂'
        } else if (this.registerForm.password.match(strongReg)) {
          this.pwdLevel = 'three'
          this.LevelCon = '很强'
        }
      }
    },
    register () {
      this.$refs.registerFormRef.validate(async valid => {
        // eslint-disable-next-line no-useless-return
        if (!valid) return
        // console.log('this.registerForm', this.registerForm)
        const data = {
          email: this.registerForm.email,
          name: this.registerForm.name,
          password: this.$md5(this.registerForm.password)
        }
        // eslint-disable-next-line no-undef
        const { data: res } = await this.$http.post('http://localhost:3007/api/reguser', this.$qs.stringify(data))
        // console.log('res', res)
        if (res.status !== 0) return this.$message.error('注册失败！')
        this.$message.success('注册成功，3秒后请完善用户信息！')
        this.$refs.registerFormRef.resetFields()
        setTimeout(() => {
          this.$router.push({ path: '/perfect' })
        }, 3000)
      })

      // this.$router.push({ path: '/perfect' })
    },
    toLogin () {
      this.$router.push({ path: '/login' })
    },
    forget () {
      this.$router.push({ path: '/forget' })
    }
  }
}
</script>

<style lang="less" scoped>
  .bg {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f7f7f7;
    overflow-y: hidden;
  }

  .login_box {
    width: 1440px;
    height: 1024px;
    background-color: #fff;
    display: flex;
  }
  .left {
    width: 720px;
    height: 1024px;
    background-color: #9A9AEB;
    position: relative;
    .img_box {
      width: 567px;
      height: 339px;
      position: absolute;
      top:293px;
      left: 99px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .right {
    width: 720px;
    height: 1024px;
    position: relative;
    p {
      position: absolute;
      width: 385px;
      height: 38px;
      top: 224px;
      right: 148px;
      margin-bottom: 0px;
      color: rgb(0, 0, 0);
      font-family: Roboto;
      font-size: 32px;
      font-weight: 500;
      line-height: 38px;
      letter-spacing: 0%;
      text-align: left;
    }
  }
  .regis_form {
    position: absolute;
    top: 301px;
    left: 127px;
    width: 446px;
    margin-top: 39px;
    .item ::v-deep .el-form-item__label {
      padding-top: 15px;
      color: rgb(0, 0, 0);
      font-family: Roboto;
      font-size: 24px;
      font-weight: 400;
      line-height: 28px;
      letter-spacing: 0%;
      text-align: left;
    }
    .el-input ::v-deep .el-input__inner {
      border:1px solid #4D4DDB;
    }
  }
  .checkbox ::v-deep .el-checkbox__label {
    padding-top: 10px;
    color: rgb(0, 0, 0);
    font-family: Roboto;
    font-size: 20px;
    font-weight: 400;
    line-height: 23px;
    letter-spacing: 0%;
    text-align: left;
  }
  .forget {
    position: absolute;
    top:884px;
    right: 147px;
      .el-button {
        padding: 0px;
        color: rgb(0, 0, 0);
        font-family: Roboto;
        font-size: 20px;
        font-weight: 400;
        line-height: 23px;
        letter-spacing: 0%;
        text-align: left;
      }
  }
  .btns {
    margin-top: 24px;
    display: flex;
    justify-content: center;
    .btns ::v-deep .el-button--primary {
      width: 446px;
      height: 55px;
    }
    .el-button {
      margin: 0px;
      width: 446px;
      height: 55px;
      background: rgb(30, 30, 30);
      border-radius: 8px;
      color: rgb(255, 255, 255);
      font-family: Roboto;
      font-size: 24px;
      font-weight: 500;
      line-height: 28px;
      letter-spacing: 0%;
      text-align: center;
    }
    .btns ::v-deep .el-button:focus, .el-button:active {
      background: #66b1ff;
      border-color: #66b1ff;
      color: #FFF;
    }
  }
  .bottom {
    position: absolute;
    width: 255px;
    height: 23px;
    top: 884px;
    right: 343px;
    text-align: center;
    span {
      font-family: Roboto;
      font-size: 20px;
      font-weight: 400;
      line-height: 23px;
      letter-spacing: 0%;
    }
    .el-button {
      padding: 0;
    }
  }
    .tip{
    margin: 0px;
    width: 125px;
    height: 23px;

    .level{
      color: rgb(0, 0, 0);
      font-family: Roboto;
      font-size: 20px;
      font-weight: 400;
      line-height: 23px;
      letter-spacing: 0%;
      text-align: left;
    }
    .one {
      color: red;
      font-family: Roboto;
      font-size: 20px;
      font-weight: 400;
      line-height: 23px;
      letter-spacing: 0%;
      text-align: left;
    }
    .two {
      color: #66c2ff;
      font-family: Roboto;
      font-size: 20px;
      font-weight: 400;
      line-height: 23px;
      letter-spacing: 0%;
      text-align: left;
    }
    .three {
      color: #00ff85;
      font-family: Roboto;
      font-size: 20px;
      font-weight: 400;
      line-height: 23px;
      letter-spacing: 0%;
      text-align: left;
    }
  }

</style>
